<template>
  <div class="my-container">
    <!-- 登录后显示的状态 -->
    <div v-if="user" class="header user-info">
      <!-- 用户信息区域 -->
      <div class="base-info">
        <!-- 头像区域 -->
        <div class="left">
          <van-image class="avatar" round fit="cover"
          :src="userInfo.photo"/>
          <span class="names">{{userInfo.name}}</span>
        </div>
        <!-- 编辑资料 -->
        <div class="right">
          <van-button
          size="mini" round
          to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <!-- 点赞关注 -->
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{-userInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>

    <!-- 未登录显示的状态 -->
    <div v-else class="header not-login">
      <div class="login-btn">
        <img class="mobile-img"
        src="~@/assets/mobile.png"
        @click="$router.push('/login')">
        <span class="text">登录 / 注册</span>
      </div>
    </div>

    <!-- 导航 宫格 -->
    <van-grid class="nav-grid" :column-num="2" clickable >
      <van-grid-item text="收藏" to="/collections">
        <i slot="icon" class="iconfont iconshoucang"></i>
        <span class="text" slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item text="文字" to="/histories">
        <i slot="icon" class="iconfont iconlishi"></i>
        <span class="text" slot="text">历史</span>
      </van-grid-item>
    </van-grid>

    <!-- 消息通知及退出 -->
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell v-if="user" class="logout-cell"
    title="退出登录" center @click="onLogout" clickable/>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user'
export default {
  name: 'MyPage',
  components: {},
  props: {},
  data () {
    return {
      // 用户自己的信息
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    // 如果用户登录了,再请求加载用户信息数据
    if (this.user) this.userList()
  },
  mounted () {},
  methods: {
    onLogout () {
      // console.log(22)
      // 退出提示
      this.$dialog.confirm({
        title: '确认退出吗?'
      })
        .then(() => {
          // on confirm
          console.log('确认')
          // 确认退出: 清除登录状态(容器中的 user +本地存储中的 user )
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
          console.log('取消')
        })
    },
    // 获取用户自己信息
    async userList () {
      try {
        const { data: res } = await getUserInfo()
        console.log(res.data)
        this.userInfo = res.data
      } catch (err) {
        this.$toast('获取数据失败,请稍后重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.my-container {
  .header {
    height: 361px;
    // 韩信白龙吟
    // background: url(https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=243724812,698053730&fm=26&gp=0.jpg) no-repeat;
    // 貂蝉仲夏夜之梦
    background: url(https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3021914512,3587454771&fm=26&gp=0.jpg) no-repeat;
    background-size: cover;
  }
  .not-login{
      display: flex;
      justify-content: center;
      align-items: center;
      .login-btn{
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        .mobile-img {
          width: 132px;
          height: 132px;
        }
        .text{
          font-size: 28px;
          color: #fff;
        }
      }
  }
  .user-info{
    .base-info{
      width: 100%;
      height: 231px;
      // background-color: red;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left{
        display: flex;
        align-items: center;
        .avatar{
          width:132px;
          height:132px;
          margin-right: 23px;
          border: 2px solid #fff;
        }
        .names{
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .data-stats{
      width: 100%;
      // height: 130px;
      // background-color: pink;
      display: flex;
      .data-item{
        height: 130px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
          color: #fff;
        .count{
          font-size: 36px;
        }
        .text{
          font-size: 23px;
        }
      }
    }
  }
  // 收藏 历史
  .nav-grid {
    background: #fff;
    z-index: 999;
    .iconfont {
      font-size: 45px;
    }
    .iconshoucang {
      color: #EB5253;
    }
    .iconlishi {
      color: #FF9D1D;
    }
    .text {
      color: #000;
      margin-top: 8px;
      font-size: 28px;
    }
  }
  // 退出登录
  .logout-cell {
    height: 100px;
    text-align: center;
    color: #EB5253;
    margin: 10px 0;
    font-size: 30px;
  }
}
</style>
